<template>
  <div>
    <div>
      <el-button @click="handleAdd" icon="CirclePlus" style="margin-left: 10px;width: 150px" type="primary">新增轮播图</el-button>
    </div>
  <el-table :data="tableData" style="width: 100%;margin-top: 20px" >
    <el-table-column label="轮播图">
      <template #default="scope">
       <img style="width: 100px" :src="genUrl(scope.row.bannerUrl)" />
      </template>
    </el-table-column>
    <el-table-column label="描述" align="center">
      <template #default="scope">
        {{ scope.row.introduce }}
      </template>
    </el-table-column>

    <el-table-column label="排序" align="center">
      <template #default="scope">
        <el-tag >{{ scope.row.sort}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="360" align="center">
      <template #default="scope">
        <el-button size="small"  @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
        <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>

    <el-pagination layout="prev, pager, next,sizes" :default-current-page="pages.page" :total="pages.total" :page-sizes="[5, 20, 30, 40, 50, 100]" :page-size="pages.limit"

                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
    />

  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="" width="500">
    <el-form :model="form">
      <el-form-item label="轮播图:" >
        <upload  v-if="dialogFormVisible" :limit="1" @change="change" :value="form.bannerUrl"></upload>
      </el-form-item>

      <el-form-item label="描述:" >
        <el-input type="textarea" v-model="form.introduce" autocomplete="off" />
      </el-form-item>
      <el-form-item label="排序(值越大越靠前):" >
        <el-input  v-model="form.sort" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="upData">
          {{form.id != undefined ? '更新' : '新增'}}
        </el-button>
      </div>
    </template>
  </el-dialog>

  </div>
</template>

<script lang="ts" setup>
import {Timer, User} from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import Upload from "@/components/Upload.vue";
import { onBeforeMount, onMounted } from "vue";
import http  from "@/util/http-util";
import {ElMessage} from "element-plus";

interface Banner {
  id: number
  bannerUrl :string
  introduce: string
  sort:number
}
const dialogFormVisible = ref(false)
const upData = () => {

  http.POST("/manage/addOrEditBanner",form.value).then((response) => {
     if (response.code == 0){
       ElMessage.success("更新成功")
       dialogFormVisible.value = false;
       loadData();
     }
  })

}

const handleSizeChange = (val: number) => {
  pages.value.limit = val;
  loadData();
}
const handleCurrentChange = (val: number) => {
  pages.value.page = val;
  loadData();
}

const handleAdd = () => {
  dialogFormVisible.value = true
  form.value = {
    id: undefined,
    bannerUrl :'',
    introduce: '',
    sort:1
  };
}

const handleEdit = (index: number, row: Banner) => {
  console.log("row",row)
  form.value = {...row};
  console.log("row2",form.value)
  dialogFormVisible.value = true
}
const handleDelete = (index: number, row: Banner) => {
  http.POST("/manage/deleteBanner",row).then((response) => {
    if (response.code == 0) {
      ElMessage.success("删除成功");
      loadData();
    }
  })
}
const change= (url)=>{
  form.value.bannerUrl = url;
}

const genUrl = (url)=>{
  return http.genUrl(url);
}

const tableData = ref<Banner[]>([

])


const form = ref<any>({
  id: undefined,
  bannerUrl :'',
  introduce: '',
  sort:1
})

const pages = ref<any>({
  total:0,
  limit:5,
  page:1,
});

const loadData = ()=>{
  console.log("ppp",pages.value)
  http.GET("/manage/getBannerList",pages.value).then((res)=>{
    tableData.value = res.data.records;
    pages.value.total =  res.data.total;
  })
}

onBeforeMount(()=>{
  loadData();
})
</script>